import Vue from 'vue'
import Vuex from 'vuex'
// // 引入封装好的模块
import menu from './menu/index'
import role from './role/index.js'
import conservator from './conservator/index.js'
import classify from './classify/index'
import sku from './sku/index'
import cmmg from './cmmg/index'
import banner from './banner/index'
import seckill from './seckill/index'
import member from './member/index'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: sessionStorage.getItem('userinfo')
      ? JSON.parse(sessionStorage.getItem('userinfo'))
      : null
  },
  getters: {
    getUser (state) {
      return state.userInfo
    }
  },
  mutations: {

    REQ_USERINFO (state, payload) {
      if (payload) {
        state.userInfo = payload
        // 把结果存储到会话存储中
        sessionStorage.setItem('userinfo', JSON.stringify(payload))
      } else {
        // 清空存储内容
        sessionStorage.removeItem('userinfo')
      }
    }
  },
  actions: {
    changeUserInfo ({ commit }, payload) {
      commit('REQ_USERINFO', payload)
    }
  },
  modules: {
    menu,
    role,
    conservator,
    classify,
    sku,
    cmmg,
    banner,
    seckill,
    member
  }
})
